<template>
  <div>
    <el-form>
      <el-form-item label="角色名称">
        <el-input v-model="role.name"></el-input>
      </el-form-item>
      <el-form-item label="角色编码">
        <el-input v-model="role.code"></el-input>
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input type="textarea" v-model="role.description"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="onCancel">取消</el-button>
        <el-button type="primary" @click="onSubmit">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import { createOrUpdate, getRoleById } from '/@/services/Role'
import { ElMessage } from 'element-plus'
export default defineComponent({
  name: 'CreateOrEdit',
  components: {},
  props: {
    roleId: {
      type: [String, Number],
      default: ''
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  setup(props: any, context: any) {
    const role = ref({
      code: '',
      name: '',
      description: ''
    })
    const onSubmit = async () => {
      const { data } = await createOrUpdate(role.value)
      if (data.code === '000000') {
        ElMessage.success('提交成功')
        context.emit('success')
      }
    }
    const onCancel = () => {
      context.emit('cancel')
    }
    const loadRole = async () => {
      const { data } = await getRoleById(props.roleId)
      role.value = data.data
    }
    return {
      role,
      onSubmit,
      onCancel,
      loadRole
    }
  },
  data() {
    return {}
  },
  created() {
    if (this.isEdit) {
      this.loadRole()
    }
  }
})
</script>
<style lang="scss" scoped></style>
